package game.leon.engine.net;

import game.leon.engine.net.codec.GateCodecFactory;
import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

import java.io.IOException;
import java.net.InetSocketAddress;

/**
 * User: bianpeng
 * Date: 11-12-27
 */
public class GateServer {
    private IoAcceptor acceptor;
    //
    // start game server
    //
    public void start(int port, GateMessageHandler messageHandler) throws IOException {
        // initialize
        acceptor = new NioSocketAcceptor();
        GateCodecFactory leonCodecFactory = new GateCodecFactory();
        acceptor.getFilterChain().addLast("logger", new LoggingFilter());
        acceptor.getFilterChain().addLast("protocol",
                new ProtocolCodecFilter(leonCodecFactory));
        acceptor.setHandler(new GateIoHandler(messageHandler));
        // start
        acceptor.bind(new InetSocketAddress(port));
    }
}
